package com.qst.controller;

import com.qst.pojo.User;
import com.qst.pojo.Userlist;
import com.qst.service.UserService;
import com.qst.service.UserlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller public class UserController {

	 @Autowired private UserService userService;
	 @Autowired private UserlistService userlistService;

	 @RequestMapping("/user/update") public String updateUserInfo(HttpSession httpSession,
			 Model model, User user, @RequestParam(value = "password1") String password1)
			 throws Exception {
		  User user1 = userService.getUser(user.getId());
		  if (user1.getPassword().equals(user.getPassword())) {
				user1.setPassword(password1);
				userService.updateUser(user1);
				model.addAttribute("msg", "密码修改成功");
		  } else {
				model.addAttribute("msg", "原密码输入错误，修改密码失败");
		  }
		  model.addAttribute("mainPage", "../common/userupdate.jsp");
		  return "common/main";
	 }

	 @RequestMapping("/user/get") public String getUserInfo(HttpSession httpSession, Model model) {
		  User user1 = (User) httpSession.getAttribute("user");
		  model.addAttribute("user", user1);
		  model.addAttribute("mainPage", "../common/userupdate.jsp");
		  return "common/main";
	 }

	 @RequestMapping("/login") public String login(HttpSession httpSession,
			 @RequestParam(value = "out", required = false) boolean out) throws Exception {
		  if (out) {
				httpSession.removeAttribute("user");
				httpSession.removeAttribute("ulist");
		  }
		  return "login";
	 }

	 @RequestMapping("/logincheck") public String login(User user, Model model,
			 HttpSession httpSession) throws Exception {
		  String url;
		  User user1 = userService.login(user);
		  if (user1 != null) {
				httpSession.setAttribute("user", user1);
				initUserlist(user1);
				Userlist list = userlistService.findhasuserlist(user1.getId());
				httpSession.setAttribute("ulist", list);
				url = "common/main";
				System.out.println("登录成功！");
		  } else {
				model.addAttribute("code", "0");
				model.addAttribute("msg", "账号或密码错误,请重新输入！");
				url = "login";
		  }
		  return url;
	 }

	 @RequestMapping("/regist") public String regist(User user, Model model, HttpSession httpSession)
			 throws Exception {
		  String url;
		  User user1 = userService.selectByUserName(user.getUsername());
		  if (user1 != null) {
				model.addAttribute("code", "0");
				model.addAttribute("msg", "账号" + user.getUsername() + "已存在！");
				url = "regist";
		  } else {
				user.setCreatedate(new Date());
				userService.insert(user);
				user=userService.selectByUserName(user.getUsername());
				initUserlist(user);
				httpSession.setAttribute("user", user);
				model.addAttribute("code", "1");
				model.addAttribute("msg", "恭喜，注册成功！");
				url = "common/main";
		  }
		  return url;
	 }

	 public void initUserlist(User user) {
		  Userlist list = userlistService.findhasuserlist(user.getId());
		  if (list == null) {
				list = new Userlist();
				list.setUser_id(user.getId());
				userlistService.insertuserlist(list);
		  }

	 }

	 @RequestMapping("/toregist") public String toRegist(Model model) throws Exception {
		  return "/regist";
	 }

	 @RequestMapping("/toindex") public String toindex(Model model) throws Exception {

		  return "common/main";
	 }

}
